/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */

package nl.dualit.clazzified.core.model.object;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.TreeMap;

import nl.dualit.clazzified.core.model.ObjektData;
import nl.dualit.clazzified.core.model.attribute.IAttribute;
import nl.dualit.clazzified.core.model.clazz.Clazz;

class Objekt extends Observable implements IObjekt {
	private Clazz clazz;
	private Map<IAttribute,ObjektData> data;
	private Long id;
	
	static IObjekt getNewInstance( Clazz clazz, List<ObjektData> data ){
		return new Objekt( clazz, data );
	}
	
	private Objekt( Clazz clazz, List<ObjektData> data ){
		// TODO If data not internal than it should be internalized.
		
		isInternalData( data );
		this.clazz = clazz;
		setData( data );
	}
	
	/**
	 * Check if data set is internallized.
	 * 
	 * @param data
	 * @return
	 */
	private boolean isInternalData( List<ObjektData> data ){
		
		for( ObjektData d : data )
			if( !d.isInternal() )
				return false;
		
		return true;
	}
	
	@Override
	public Clazz getClazz() {
		return clazz;
	}

	@Override
	public Object getData(IAttribute attribute) {
		for( ObjektData objData : data.values() ){
			if( objData.getAttribute().equals( attribute ) ){
				return objData;
			}
		}
		return null;
	}

	@Override
	public void setData( IAttribute attribute, Object value ){
		data.put( attribute, ObjektData.getInstance(attribute, value) );
		
		setChanged();
		notifyObservers( data );
	}
	
	
	@Override
	public List<ObjektData> getData() {
		return Collections.unmodifiableList( new ArrayList<ObjektData>( data.values() ) );
	}

	@Override
	public List<IAttribute> getAttributes() {
		return clazz.getAttributes();
	}

	@Override
	public void resetData( ) {
		this.data.clear();
		setChanged();
		notifyObservers( data );
	}

	static Map<IAttribute,Object> convert( List<ObjektData> data ){
		Map<IAttribute,Object> result = new TreeMap<IAttribute, Object>();
		
		return result;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setData(Map<IAttribute, Object> data) {
		for( IAttribute attribute : data.keySet() ){
			this.data.put( attribute, 
					ObjektData.getInstance( attribute, data.get( attribute )  )
			);
		}
		
	}

	public void setData( List<ObjektData> Objdata ){
		for( ObjektData od : Objdata ){
			if( od.isInternal() )
				this.data.put( od.getAttribute(),  od );
			else // TODO Internalize
				throw new IllegalStateException("Method not fully implemented");
				
		}
	}
	@Override
	public void setId(Long id) {
		// TODO Auto-generated method stub
		
	}
	
}
